package com.qdu.study_app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;

import com.qdu.study_app.MyActivity;
import com.qdu.study_app.R;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;

public class Activity_splash extends MyActivity {

    private static final int sleepTime = 3000;
    private Thread thread = null;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        textView = findViewById(R.id.jump_button);
        CountDownTimer timer = new CountDownTimer(3000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                textView.setText(millisUntilFinished / 1000 + "跳过");
            }

            @Override
            public void onFinish() {
                textView.setText("0s 跳过");
            }
        };

        timer.start();
        // Bmob云初始化
        Bmob.initialize(this,"e01815c652c8624e0928fec6bd12e561");
    }


    @Override
    protected void onStart() {
        super.onStart();
        thread = new Thread(() -> {
            long start = System.currentTimeMillis();
            long costTime = System.currentTimeMillis() - start;
            if(sleepTime -costTime > 0){
                try {
                    Thread.sleep(sleepTime - costTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if(!thread.isInterrupted()) {
                // 如果用户未登录
                if(BmobUser.getCurrentUser() == null){
                    startActivity(new Intent(Activity_splash.this, Login_first.class));
                    finish();
                }else{
                    // 如果用户已登录
                    startActivity(new Intent(Activity_splash.this,Activity_index.class));
                    finish();
                }

            }
        });
        thread.start();
    }
   public void jumpToMain(View view){
   //     startActivity(new Intent(Activity_splash.this,MainActivity.class));
        thread.interrupt();
        finish();
    }
}